package lp.chess.ui;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JPanel;
import java.awt.Graphics;


public class JCanvas extends JPanel {
	private static final long serialVersionUID = -7719437868940499338L;
	
	private List<IDrawable> m_drawables = new LinkedList<IDrawable>();
	
	public synchronized void paint(Graphics g) {
		for (Iterator<IDrawable> iter = m_drawables.iterator(); iter.hasNext();) {
			IDrawable d = (IDrawable) iter.next();
			d.draw(g);	
		}
	}

	public synchronized void addDrawable(IDrawable d) {
		m_drawables.add(d);
		repaint();
	}
	
	public synchronized void addDrawables(List<IDrawable> drawables) {
		for (Iterator<IDrawable> iter = drawables.iterator(); iter.hasNext();) {
			IDrawable d = (IDrawable) iter.next();
			m_drawables.add(d);
		}
		
		repaint();
	}

	public synchronized void removeDrawable(IDrawable d) {
		m_drawables.remove(d);
		repaint();
	}
	
	public synchronized void removeDrawables(List<IDrawable> drawables) {
		System.out.println(m_drawables.size());
		
		for (Iterator<IDrawable> iter = drawables.iterator(); iter.hasNext();) {
			IDrawable d = (IDrawable) iter.next();
			m_drawables.remove(d);
		}
		
		repaint();
		
		System.out.println(" " + m_drawables.size());
	}

	public synchronized void clear() {
		m_drawables.clear();
		repaint();
	}

	
}